/*
                  PlayersetupFrame Class Implementation
*/
#include "PlayersetupFrame.h"

PlayersetupFrame::PlayersetupFrame()
{

}

PlayersetupFrame::~PlayersetupFrame()
{

}

int PlayersetupFrame::Initialise(Point NewPosition, int iNewWidth, int iNewHeight)
{
    Position = NewPosition;

    strName = "";

    iWidth = iNewWidth;
    iHeight = iNewHeight;

    chkPlaying = new Checkbox;
    chkPlaying->Initialise(Point(25, 55), "Data//Fonts//bold.ttf", 18, "data//interface//check.png", "data//interface//uncheck.png", "Playing?", GetSDLColor(255, 255, 255));
    chkPlaying->setChecked(true);

    lblType = new Label;
    lblType->Initialise(Point(25, 15), "Data//Fonts//bold.ttf", 18, "Player Type: ", GetSDLColor(255, 255, 255));

    lblPlayer = new Label;
    lblPlayer->Initialise(Point(200, 55), "Data//Fonts//bold.ttf", 18, "Player: ", GetSDLColor(255, 255, 255));

    vector<string> Types;

    Types.push_back("Local");
    Types.push_back("Player");
    Types.push_back("CPU");

    lstType = new Listbox;
    lstType->Initialise(Point(200, 15), 18, "Data//Fonts//bold.ttf", 100, 0xFFFFFF, GetSDLColor(0, 0, 0));
    lstType->SetListItems(Types);

    this->AddWidget(chkPlaying, "chkPlaying");
    this->AddWidget(lblType, "lblType");
    this->AddWidget(lblPlayer, "lblPlayer");

    InactiveBG = IMG_Load("data//interface//gamelobby//playersetup_blank.png");
}

bool PlayersetupFrame::CheckFocus(int iMouseX, int iMouseY)
{
    if (bVisible == false)
    {
        return false;
    }

    double xpos = Position.x;
    double ypos = Position.y;

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        return true;
    }
    else
    {
        if (lstType->getExpanded() == true)
        {
            return true;
        }

        return false;
    }
}

void PlayersetupFrame::setName(const string& strNewName)
{
    lblPlayer->setText("Player: " + strNewName);

    strName = strNewName;
}

string PlayersetupFrame::getName()
{
    return strName;
}

int PlayersetupFrame::HandleMouseDown(int iMouseX, int iMouseY)
{
    double xpos = Position.x;
    double ypos = Position.y;

    bool bWidgetEvent = false;

    if (lstType->getExpanded() == true)
    {
        lstType->HandleMouseDown(iMouseX - (int)Position.x, iMouseY - (int)Position.y);

        EventBuffer.push(new GUIEvent(GUIEVENT_LEFTMOUSEDOWN, lstType->getName()));
    }

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        if (bVisible == true)
        {
            for (int i = 0; i < Widgets.size(); i++)
            {
                if (Widgets.at(i)->getEnabled() == true && Widgets.at(i)->getVisible() == true)
                {
                    if (Widgets.at(i)->HandleMouseDown(iMouseX - (int)Position.x, iMouseY - (int)Position.y) == 1)
                    {
                       EventBuffer.push(new GUIEvent(GUIEVENT_LEFTMOUSEDOWN, Widgets.at(i)->getName()));

                       bWidgetEvent = true;
                    }
                }
            }

            if (bFixed == false)
            {
                if (bWidgetEvent == false)
                {
                    bDragging = true;
                }
            }

            if (lstType->getExpanded() == false)
            {
                lstType->HandleMouseDown(iMouseX - (int)Position.x, iMouseY - (int)Position.y);

                EventBuffer.push(new GUIEvent(GUIEVENT_LEFTMOUSEDOWN, lstType->getName()));
            }
        }
    }

    return 0;
}

int PlayersetupFrame::BlitToSurface(SDL_Surface *surface)
{
    if (bVisible == true)
    {
        if (chkPlaying->getChecked())
        {
            if (Background != NULL)
            {
                SDL_Rect dest;
                dest.x = static_cast<int>(Position.x);
                dest.y = static_cast<int>(Position.y);
                SDL_BlitSurface(Background, NULL, surface, &dest);
            }
        }
        else
        {
            if (InactiveBG != NULL)
            {
                SDL_Rect dest;
                dest.x = static_cast<int>(Position.x);
                dest.y = static_cast<int>(Position.y);
                SDL_BlitSurface(InactiveBG, NULL, surface, &dest);
            }
        }

        SDL_Rect clip;

        clip.x = static_cast<int>(Position.x);
        clip.y = static_cast<int>(Position.y);
        clip.w = iWidth;
        clip.h = iHeight;

        // Stop widgets overflowing the frame area
        SDL_SetClipRect(surface, &clip);

        for (int i = 0; i < Widgets.size(); i++)
        {
            Widgets.at(i)->setFramePosition(Position);

            if (Widgets.at(i)->getVisible() == true)
            {
                Widgets.at(i)->BlitToFrame(surface, Position);
            }
        }

        // Reset the screen clip rect to the full surface
        SDL_SetClipRect(surface, NULL);

        lstType->BlitToFrame(surface, Position);
    }
}
